package org.example.server.impl;

import org.example.mapper.DeptsMapper;
import org.example.mapper.EmpMapper;
import org.example.pojo.Depts;
import org.example.server.DeptServer;
import org.example.server.LogServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DeptServerImpl implements DeptServer {

    @Autowired
    private DeptsMapper deptsMapper;

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private LogServer logServer;

    @Override
    public List<Depts> list() {
        return deptsMapper.list();
    }

    // 添加事务注解
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer id) {
        String deptName = deptsMapper.getById(id).getName();
        try {
            deptsMapper.delete(id);
            empMapper.deleteByDeptId(id);
        }finally {
            logServer.addDeleteLog("删除了部门："+ id + ":" + deptName);
        }

    }

    @Override
    public void add(Depts depts) {
        deptsMapper.add(depts);
    }

    @Override
    public Depts getById(Integer id) {
        return deptsMapper.getById(id);
    }

    @Override
    public void update(Depts depts) {
        deptsMapper.update(depts);
    }
}
